<template>
    <div class="components_edit_modal">
        <el-dialog title="添加字典" :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <form>
            </form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="small" @click="addHomework">确 定</el-button>
                <el-button @click="state = false"  size="small">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {enums} from '@/constant'
    import {config} from "../../../../config.js";
    export default {
        data() {

            return {
                state:false,
                queryFormData:{
                    classIds:[],
                    homework:{
                        title:null,
                        type:'0',
                        courseNo:null,
                        courseName:null,
                        proportion:null,
                        startTime:null,
                        endTime:null,
                        remark:null,
                    },
                    homeworkAssessList:[{
                        appraisalName:null,
                        score:null,
                        remark:null
                    }],
                },
                rules: {
                        title: [
                            { required: true, message: '作业名称不能为空', trigger: 'blur'},
                            { min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur' }
                        ],
                        courseNo: [
                            { required: true, message: '请选择课程', trigger: 'change'},
                        ],
                        proportion: [
                            { required: true, message: '请填写正确的成绩占比', trigger: 'blur',type:"number"},
                            // { min: 0, max: 100, message: '只能输入大于0小于100的数（含整数、两位小数）', trigger: 'blur', type:'number',
                            //     transform(value){
                            //         if(!isNaN(value)){
                            //             value = Number(value)
                            //             return value;
                            //         }
                            //         return "";
                            //     }
                            // }
                        ],
                        startTime: [
                            { required: true, message: '请选择开始时间',trigger: 'change',type:"date"},
                            { validator: checkstartTime, trigger: "blur" }
                        ],
                        endTime: [
                            { required: true, message: '请选择结束时间',trigger: 'change',type:"date"},
                            { validator: checkendTime, trigger: "blur" }
                        ],
                        remark: [
                            { min: 2, max: 300, message: '长度在 2 到 300 个字符', trigger: 'blur' }
                        ],

                    },
            }
        },
        created() {

        },
        mounted() {},
        methods: {
            showDialog(){
                 if(this.$refs['queryFormData']!==undefined){
                    this.$refs['queryFormData'].resetFields();
                }
                this.state = true;
                this.attachment = [];
                this.classeList = [];
                this.queryFormData={
                    classIds:[],
                    homework:{
                        title:null,
                        type:'0',
                        courseNo:null,
                        courseName:null,
                        proportion:null,
                        startTime:null,
                        endTime:null,
                        remark:null,
                    },
                    homeworkAssessList:[{
                        appraisalName:null,
                        score:null,
                        remark:null
                    }],
                };
                this.getCourseList();
            },
            reloadFiles: function (files) {
                let vm = this;
                this.attachment = [];
                files.forEach((item,idnex)=>{
                    let model = {
                        fileRealName: item.fileRealName,
                        objectKey: item.objectKey
                    }
                    this.attachment.push(model)
                })
            },
            getCourseList(){
                let self = this;
                self.axios.get(api.homeworkTeacher.initData).then((response) => {
                    if (response.data.success) {
                        self.courseList = JSON.parse(JSON.stringify(response.data.data.courses));
                        // self.classeList = JSON.parse(JSON.stringify(response.data.data.classes));
                    } else {
                        self.$message.error(response.data.msg);
                    }
                })
            },
            changeClassList(courseNo){
                let self = this;
                self.axios.get(api.queryTrainPlanClassByUserId,{
                    params: {
                        courseNo:courseNo
                    }
                }).then((response) => {
                    if (response.data.success) {
                        // console.log(courseNo+"-->"+JSON.stringify(response.data.data));
                        self.classeList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        self.$message.error(response.data.msg);
                    }
                })
            },
            //添加作业
            addHomework(){
                let self = this;

                this.$refs['queryFormData'].validate((valid) => {
                    if (valid) {

                        }else{
                            self.$message.error('请选择班级！');
                        }

                    } else {
                        return false;
                    }
                });



            },
            getCourseNameAndSemesterBycourseNo(courseNo){
                let courseList = this.courseList;
                for(let i=0;i<courseList.length;i++){
                    if(courseList[i].courseNo == courseNo){
                        return (courseList[i].courseName+","+courseList[i].semester).split(",");
                    }
                }
            },
            addAssess(){
                this.queryFormData.homeworkAssessList.push({
                        appraisalName:null,
                        score:null,
                        remark:null
                })
            },
            deleteAssess(index){
                let homeworkAssessListCurrent = [];
                let homeworkAssessList = this.queryFormData.homeworkAssessList;
                for(let i=0;i<homeworkAssessList.length;i++){
                    if(i != index){
                        homeworkAssessListCurrent.push(homeworkAssessList[i]);
                    }
                }
                this.queryFormData.homeworkAssessList = homeworkAssessListCurrent;
            }
        },


    };
</script>
<style scoped>
</style>